package com.bestcem.xm.award.service.dto.draw;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.bestcem.xm.award.entity.pojo.AwardRecordDO;
import com.bestcem.xm.award.enums.AwardIssueStatusEnum;
import com.bestcem.xm.award.enums.DeliverSourceEnum;
import com.bestcem.xm.award.enums.RecordStatusEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Objects;

import static com.bestcem.xm.award.constant.Constants.AWARD_EXCEL_DEFAULT_COLUMN_WIDTH;
import static com.bestcem.xm.award.constant.Constants.AWARD_LEVELS;

/**
 * 导出中奖名单信息DTO
 *
 * @author hang.li <hang.li@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/14 14:21
 */
@Data
@Accessors(chain = true)
@ColumnWidth(AWARD_EXCEL_DEFAULT_COLUMN_WIDTH)
public class ExportWinnersDTO {

    /**
     * 答卷编号
     */
    @ExcelProperty(value = "答卷编号")
    private Long surveySeq;

    /**
     * 中奖状态
     */
    @ExcelProperty(value = "中奖状态")
    private String status;

    /**
     * 投放方式
     */
    @ExcelProperty(value = "投放方式")
    private String deliverSource;

    /**
     * 投放名称
     */
    @ExcelProperty(value = "投放名称")
    private String deliverName;

    /**
     * 奖项等级
     */
    @ExcelProperty(value = "奖项等级")
    private String level;

    /**
     * 奖项名称
     */
    @ExcelProperty(value = "奖项名称")
    private String winName;

    /**
     * 领取时间
     */
    @ExcelProperty(value = "领取时间")
    private String receiveTime;

    /**
     * 中奖人手机号
     */
    @ExcelProperty(value = "中奖人手机号")
    private String phone;

    /**
     * 中奖人姓名
     */
    @ExcelProperty(value = "中奖人姓名")
    private String name;

    /**
     * 中奖人券码信息(兑奖码)
     */
    @ExcelProperty(value = "中奖人券码信息")
    private String code;

    /**
     * 中奖人地址
     */
    @ExcelProperty(value = "中奖人地址")
    private String address;

    /**
     * 导出抽奖名单信息数据转换
     *
     * @param record 抽奖记录数据
     * @return ExportWinnersDTO
     */
    public static ExportWinnersDTO awardRecordToExportWinnersDTO(AwardRecordDO record) {
        ExportWinnersDTO winnersDTO = new ExportWinnersDTO();
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        winnersDTO.setName(record.getName())
                .setPhone(record.getPhone());
        if (record.getStatus() == RecordStatusEnum.YES.getIndex()) {
            winnersDTO.setReceiveTime(format.format(record.getUpdateTime()));
        }
        if (Objects.nonNull(record.getAward())) {
            winnersDTO.setSurveySeq(record.getAward().getSurveySeq());
            if (Objects.nonNull(record.getAward().getDeliverSource())) {
                winnersDTO.setDeliverSource(DeliverSourceEnum.getDeliverNameByIndex(record.getAward().getDeliverSource()));
            }
            winnersDTO.setDeliverName(record.getAward().getDeliverName());
        }
        if (Objects.nonNull(record.getWin())) {
            winnersDTO.setWinName(record.getWin().getName());
            if (StringUtils.isNotBlank(record.getWin().getLevelStr())) {
                winnersDTO.setLevel(record.getWin().getLevelStr());
            } else {
                winnersDTO.setLevel(AWARD_LEVELS[record.getWin().getLevel()]);
            }
            if (Objects.nonNull(record.getWin().getCode())) {
                winnersDTO.setCode(record.getWin().getCode());
            }
        }
        if (record.getStatus() == RecordStatusEnum.YES.getIndex()) {
            if (record.getWin().getStatus() == AwardIssueStatusEnum.YES.getIndex()) {
                winnersDTO.setStatus("已发奖");
            } else {
                winnersDTO.setStatus("已中奖");
            }
        } else {
            winnersDTO.setStatus("未中奖");
        }
        if (Objects.nonNull(record.getAddress())) {
            winnersDTO.setAddress((Objects.isNull(record.getAddress().getProvince()) ? "" : record.getAddress().getProvince())
                    + (Objects.isNull(record.getAddress().getCity()) ? "" : record.getAddress().getCity())
                    + (Objects.isNull(record.getAddress().getMore()) ? "" : record.getAddress().getMore()));
        }
        return winnersDTO;
    }
}